<?php

require_once('BaseModel.php');

class Usuarios extends BaseModel{
	protected $_name =  "usuarios";
	protected $userType;

	public function init(){
		parent::init();
	}

	/**
	 * @return the $_name
	 */
	public function getName() {
		return $this->_name;
	}

	function getAllBySite( $sites_id = NULL, $parent = false ){
		$select = $this->getSelectObject()
				  ->where("tipo = ?", $this->getUserType() );
		
		if( $parent )
			$select->orWhere('sites_parent_id = ?', $this->_logged->sites_id);
				  
				 
		$this->setSelectObject( $select );

		return parent::getAllBySite( $sites_id );
	}

	public function getById( $id, $pKey = 'id', $sites_id = NULL, $parent = false ){
		$select = $this->getSelectObject()
				  ->where("tipo = ?", $this->getUserType() );

		if( $parent )
			$select->orWhere('sites_parent_id = ?', $this->_logged->sites_id);

		$this->setSelectObject( $select );

		return parent::getById( $id, $pKey, $sites_id );
	}
	/**
	 * @param $userType the $userType to set
	 */
	public function setUserType($userType) {
		$this->userType = $userType;
	}

	/**
	 * @return the $userType
	 */
	public function getUserType() {
		return $this->userType;
	}
	
	/**
	 * Verifica se o login ja esta associado ao site
	 * @param $login
	 * @param $site_id
	 * @return unknown_type
	 */
	public function verifyLoginOnSite( $login, $site_id = NULL ){
		if( !$site_id )
			$site_id = $this->_logged->sites_id;

		$select = $this->getSelectObject()
				  ->where("tipo = ?", $this->getUserType() );
				  
		$query = $select
			->where( 'login = ?', $login )
			->where( 'status != ?', '2')
			->where( 'sites_id = ?', $site_id);

		$result = $this->fetchAll( $query )->toArray();	
		
		return ( $result );	
	}
}